/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.mer.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.mer.entity.Sku;
import com.kaaction.modules.mer.service.SkuService;

/**
 * 商品种类Controller
 * @author huanghaiming
 * @version 2017-05-21
 */
@Controller
@RequestMapping(value = "${adminPath}/mer/sku")
public class SkuController extends BaseController {

	@Autowired
	private SkuService skuService;
	
	@ModelAttribute
	public Sku get(@RequestParam(required=false) String id) {
		Sku entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = skuService.get(id);
		}
		if (entity == null){
			entity = new Sku();
		}
		return entity;
	}
	
	@RequiresPermissions("mer:sku:view")
	@RequestMapping(value = {"list", ""})
	public String list(Sku sku, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Sku> page = skuService.findPage(new Page<Sku>(request, response), sku); 
		model.addAttribute("page", page);
		return "modules/mer/skuList";
	}

	@RequiresPermissions("mer:sku:view")
	@RequestMapping(value = "form")
	public String form(Sku sku, Model model) {
		model.addAttribute("sku", sku);
		return "modules/mer/skuForm";
	}

	@RequiresPermissions("mer:sku:edit")
	@RequestMapping(value = "save")
	public String save(Sku sku, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, sku)){
			return form(sku, model);
		}
		skuService.save(sku);
		addMessage(redirectAttributes, "保存商品种类成功");
		return "redirect:"+Global.getAdminPath()+"/mer/sku/?repage";
	}
	
	@RequiresPermissions("mer:sku:edit")
	@RequestMapping(value = "delete")
	public String delete(Sku sku, RedirectAttributes redirectAttributes) {
		skuService.delete(sku);
		addMessage(redirectAttributes, "删除商品种类成功");
		return "redirect:"+Global.getAdminPath()+"/mer/sku/?repage";
	}

}